package com.example.parking.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.parking.dto.Result;
import com.example.parking.entity.Garage;
import com.example.parking.mapper.GarageMapper;
import com.example.parking.service.IGarageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/garage")
public class GarageController {

    @Resource
    private IGarageService garageService;

    @Resource
    private GarageMapper garageMapper;

    @GetMapping("/findFree")
    public Result findFree(){
        List<Garage> free = garageMapper.findFree();
        if (free.isEmpty()){
            return Result.fail("该停车场没有空余车位");
        }
        return Result.success();
    }

    /**
     * 分页查询车位信息
     * @return
     */
    @GetMapping("/getGarage")
    public Result getGarage(@RequestParam Integer pageNum,
                            @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        Page<Garage> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Garage> wrapper = new QueryWrapper<>();
        Page<Garage> garagePage = garageService.page(page, wrapper);
        return Result.successData(garagePage);
    }
}
